package com.bolt.auth.security.admin.biz.sys.controller;


import com.bolt.auth.admin.api.sys.dto.UserDTO;
import com.bolt.auth.admin.api.sys.dto.UserPasswordDTO;
import com.bolt.auth.admin.api.sys.dto.UserQueryCriteria;
import com.bolt.auth.security.admin.biz.sys.service.UserService;
import com.bolt.auth.security.common.utils.SecurityUtils;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.convention.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/sys/users")
public class UserContorller {

    @Autowired
    private UserService userService;

    @ApiOperation("查询用户")
    @GetMapping
    public ResultMessage page(Pageable pageable, UserQueryCriteria userDTO) {
        PageInfo<UserDTO> data = userService.findPage(pageable, userDTO);
        return Results.success(data);
    }

    @ApiOperation("用户信息新增修改操作")
    @PostMapping
    public ResultMessage create(@Validated @RequestBody UserDTO userDTO) {
        userService.saveOrUpdate(userDTO);
        return Results.success();
    }

    @ApiOperation("用户信息新增修改操作")
    @PutMapping
    public ResultMessage update(@Validated(UserDTO.Update.class) @RequestBody UserDTO userDTO) {
        userService.saveOrUpdate(userDTO);
        return Results.success();
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    public ResultMessage delete(@RequestBody Set <String> ids) {
        for (String id : ids) {
            userService.deleteUser(id);
        }
        return Results.success();
    }

    @ApiOperation("修改用户：个人中心")
    @PutMapping(value = "center")
    public ResultMessage updateCenter(@Validated(UserDTO.Update.class) @RequestBody UserDTO resources) {
        if (!resources.getId().equals(SecurityUtils.getUser().getUserId())) {
            throw new ServiceException("不能修改他人资料");
        }
       // userService.updateCenter(resources);
        return Results.success();
    }


    @ApiOperation("重置用户密码为系统默认密码")
    @PostMapping(value = "/resetPassword")
    public ResultMessage resetPassword(@RequestBody Set <String> ids) {
        for (String id : ids) {
         //   userService.resetPassword(id);
        }
        return Results.success();
    }

    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePassword")
    public ResultMessage updatePassword(@RequestBody UserPasswordDTO passDto) throws Exception {
       // String oldPassword = RSAUtil.decryptByPrivateKey(RsaProperties.privateKey, passDto.getOldPass());
       // String newPassword = RSAUtil.decryptByPrivateKey(RsaProperties.privateKey, passDto.getNewPass());
       // userService.updatePassword(oldPassword, newPassword);
        return Results.success();
    }

    @ApiOperation("修改头像")
    @PostMapping(value = "/updateAvatar")
    public ResultMessage updateAvatar(@RequestParam MultipartFile avatar) {
        //userService.updateAvatar(avatar)
        return Results.success();
    }


    @ApiOperation("修改邮箱")
    @PostMapping(value = "/updateEmail/{code}")
    public ResultMessage updateEmail(@PathVariable String code, @RequestBody UserDTO user) throws Exception {
      //  String password = RSAUtil.decryptByPrivateKey(RsaProperties.privateKey, user.getPassword());
       // userService.updateEmail(code, password, user.getEmail());
        return Results.success();
    }

}

 